# ---------------------------------------------------------------------------------------------------- #
# finalcode.R
# ---------------------------------------------------------------------------------------------------- #
#
# Date: 2014-06-18
# Authors: Jaime E. Settle, Robert M. Bond, Lorenzo Coviello, Christopher J. Fariss, James H. Fowler, Jason J. Jones, Adam D. I. Kramer, Cameron Marlow
#
# All inquires about the models and code should be sent to Jaime Settle and Chris Fariss
# Contact: jsettle@wm.edu cjf0006@gmail.com
#
# Title:From Posting to Voting: The Effects of Political Competition on Online Political Engagement
# Most recent version available at: http://ssrn.com/abstract=2232099
#
# Journal: Political Science Research & Methods
#
# Copyright (c) 2014, The R code is under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
# For more information see: http://creativecommons.org/licenses/by-nc-sa/3.0/us/
# All rights reserved.
#
# The data are available by request though Facebook Inc. Interested parties need to contact Facebook Inc. directly to obtain secure access to the data.
#
# R code begins below
# ---------------------------------------------------------------------------------------------------- #
#
#
#
#
#-----------------Start New File here-----------------#

# Navigate to be in JaimeAnalysisFiles/Voting folder on the sever

# this is just for calculating the amount of time this script takes to execute
d <- Sys.time()

# load data
load(file="/home/jfowler/Discussion/JaimeAnalysisFiles/Voting/addingcampaignvariables.RData")
load(file="/home/jfowler/Discussion/JaimeAnalysisFiles/Voting/vote.rda")


#-----------------------------Campaign Period---------------#

# create binary variables for analysis in the other R scripts 
vote$campaigntotal <- NA
vote$campaigntotal <- userpostscampaign$Freq[match(vote$userid, userpostscampaign$userid)]
vote$campaigntotal[which(is.na(vote$campaigntotal))] <- 0

vote$campaignpol75posts <- NA
vote$campaignpol75posts <- userpol75campaign$Freq[match(vote$userid, userpol75campaign$userid)]
vote$campaignpol75posts[which(is.na(vote$campaignpol75posts))] <- 0
vote$campaignpol75prop <- vote$campaignpol75posts/vote$campaigntotal
vote$campaignpol75dic <- NA
vote$campaignpol75dic[which(vote$campaignpol75posts==0)] <-0
vote$campaignpol75dic[which(vote$campaignpol75posts>=1)] <-1


vote$campaignemopol75posts <- NA
vote$campaignemopol75posts <- useremopol75campaign$Freq[match(vote$userid, useremopol75campaign$userid)]
vote$campaignemopol75posts[which(is.na(vote$campaignemopol75posts))] <- 0
vote$campaignemopol75prop <- vote$campaignemopol75posts/vote$campaignpol75posts
vote$campaignemopol75dic <- NA
vote$campaignemopol75dic[which(vote$campaignemopol75posts==0)] <-0
vote$campaignemopol75dic[which(vote$campaignemopol75posts>=1)] <-1

vote$campaignemopospol75posts <- NA
vote$campaignemopospol75posts <- useremopospol75campaign$Freq[match(vote$userid, useremopospol75campaign$userid)]
vote$campaignemopospol75posts[which(is.na(vote$campaignemopospol75posts))] <- 0
vote$campaignemopospol75prop <- vote$campaignemopospol75posts/vote$campaignpol75posts
vote$campaignemopospol75dic <- NA
vote$campaignemopospol75dic[which(vote$campaignemopospol75posts==0)] <-0
vote$campaignemopospol75dic[which(vote$campaignemopospol75posts>=1)] <-1

vote$campaignemonegpol75posts <- NA
vote$campaignemonegpol75posts <- useremonegpol75campaign$Freq[match(vote$userid, useremonegpol75campaign$userid)]
vote$campaignemonegpol75posts[which(is.na(vote$campaignemonegpol75posts))] <- 0
vote$campaignemonegpol75prop <- vote$campaignemonegpol75posts/vote$campaignpol75posts
vote$campaignemonegpol75dic <- NA
vote$campaignemonegpol75dic[which(vote$campaignemonegpol75posts==0)] <-0
vote$campaignemonegpol75dic[which(vote$campaignemonegpol75posts>=1)] <-1

vote$campaignemoanxpol75posts <- NA
vote$campaignemoanxpol75posts <- useremoanxpol75campaign$Freq[match(vote$userid, useremoanxpol75campaign$userid)]
vote$campaignemoanxpol75posts[which(is.na(vote$campaignemoanxpol75posts))] <- 0
vote$campaignemoanxpol75prop <- vote$campaignemoanxpol75posts/vote$campaignpol75posts
vote$campaignemoanxpol75dic <- NA
vote$campaignemoanxpol75dic[which(vote$campaignemoanxpol75posts==0)] <-0
vote$campaignemoanxpol75dic[which(vote$campaignemoanxpol75posts>=1)] <-1

vote$campaignemoangpol75posts <- NA
vote$campaignemoangpol75posts <- useremoangpol75campaign$Freq[match(vote$userid, useremoangpol75campaign$userid)]
vote$campaignemoangpol75posts[which(is.na(vote$campaignemoangpol75posts))] <- 0
vote$campaignemoangpol75prop <- vote$campaignemoangpol75posts/vote$campaignpol75posts
vote$campaignemoangpol75dic <- NA
vote$campaignemoangpol75dic[which(vote$campaignemoangpol75posts==0)] <-0
vote$campaignemoangpol75dic[which(vote$campaignemoangpol75posts>=1)] <-1


# calculate the amount of time the file took to execute
Sys.time()-d

vote2 <- vote

# save the data
save(vote2, file="/home/jfowler/Discussion/JaimeAnalysisFiles/Voting/vote2.rda")

#--------------------End of File-----------------------#
